package com.design.movieticket.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.design.movieticket.common.base.constant.Result;
import com.design.movieticket.common.base.constant.ResultStatus;
import com.design.movieticket.common.dto.XPage;
import com.design.movieticket.entity.Coupon;
import com.design.movieticket.entity.Coupon;
import com.design.movieticket.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Api(tags = "优惠卷模块")
@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Resource
    private CouponService couponService;

    /** 分页查询 */
    @ApiOperation("优惠卷信息分页展示")
    @GetMapping("/list/page")
    public Result findCouponListByPage(Coupon coupon, XPage<Void> page) {
        IPage<Coupon> CouponList = couponService.page(XPage.toIPage(page), couponService.buildQuery(coupon));
        return new Result(CouponList, ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 新增或更新优惠卷数据 */
    @ApiOperation("新增或更新优惠卷数据")
    @PostMapping("/addOrUpdate")
    public Result addOrEditCoupon(@RequestBody Coupon coupon) {
        couponService.addOrUpdateCouponInfo(coupon);
        return new Result(ResultStatus.SAVE_SUCCESS.getMsg(),ResultStatus.SAVE_SUCCESS.getCode());
    }

    /** 获取优惠卷信息 */
    @ApiOperation("获取优惠卷信息详情")
    @GetMapping("/getCouponDetail/{id}")
    public Result getCouponDetail(@PathVariable long id){
        return new Result(couponService.getCouponDetail(id),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 删除优惠卷信息 */
    @ApiOperation("删除优惠卷信息")
    @PostMapping("/delCouponById/{id}")
    public Result delCouponById(@PathVariable long id){
        couponService.delCouponById(id);
        return new Result(ResultStatus.DELETE_SUCCESS.getMsg(),ResultStatus.DELETE_SUCCESS.getCode());
    }





    /** 小程序获取优惠券列表 */
    @GetMapping("/wxGetCouponList")
    public Result wxGetCouponList(){
        return new Result(couponService.wxGetCouponList(),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 小程序获取持有优惠券 */
    @GetMapping("/wxGetMyCouponList")
    public Result wxGetMyCouponList(Long userId){
        return new Result(couponService.wxGetMyCouponList(userId),ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }

    /** 小程序兑换优惠券 */
    @GetMapping("/conversionCoupon")
    public Result wxConversionCoupon(Long couponId,Long userId){
        String result = "积分不足,兑换失败";
        System.out.println("id是"+couponId);
        System.out.println("id是"+userId);
        Coupon coupon = couponService.getCouponDetail(couponId);
        if (couponService.wxConversionCoupon(coupon,userId) == 1){
            result = "兑换成功";
        }
        return new Result(result,ResultStatus.REQUEST_SUCCESS.getMsg(),ResultStatus.REQUEST_SUCCESS.getCode());
    }
}
